package com.orangelife.mobile.voucher.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.TextView;

import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.ToastHelper;
import com.curry.orangelife.mobile.R;
import com.markupartist.android.widget.PullToRefreshListView;
import com.orangelife.mobile.common.activity.PullRefreshBaseActivity;
import com.orangelife.mobile.common.view.ConfirmDialog;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.surrounding.biz.SurroundHttpBiz;
import com.orangelife.mobile.util.BLog;
import com.orangelife.mobile.util.JsonReponseHandler;
import com.orangelife.mobile.voucher.adapter.VoucherListSelectAdapter;
import com.orangelife.mobile.voucher.biz.VoucherBiz;
import com.orangelife.mobile.voucher.biz.VoucherHttpBiz;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我的抵用券
 *
 * @author wgf
 */
public class MyVoucherListActivity extends PullRefreshBaseActivity {
    /** 标题中间 */
    private TextView tvTitle;
    /** 标题右边 */
    private TextView tvTitleRight;
    /** 列表 */
    private PullToRefreshListView pullListView;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;
    private VoucherListSelectAdapter<Map<String, Object>> adapter;
    private List<Map<String, Object>> data;
    private int removeListViewChildIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myvoucher_list);
        ButterKnife.bind(this);
        initView();
        setListener();
        setAdapter();
        setXListViewAndEmptyLayoutListener(pullListView, emptyLayout, adapter);
    }

    @Override
    protected void requestWhenPullRefresh() {
        VoucherHttpBiz.getMyList(pageNo, pageSize, handler);
    }

    private void initView() {
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitleRight = (TextView) findViewById(R.id.tv_title_right);
        pullListView = (PullToRefreshListView) findViewById(R.id.lvVoucher);
        tvTitle.setText(R.string.my_voucher);
        tvTitleRight.setText(R.string.rule);
    }

    private void setListener() {
        pullListView.setOnItemLongClickListener(longListener);
        tvTitleRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpToVoucherRule();
            }
        });
    }

    private void jumpToVoucherRule() {
        VoucherBiz.jumpToVoucherRule(this);
    }

    private void setAdapter() {
        data = new ArrayList<>();
        adapter = new VoucherListSelectAdapter<>(this,
                data);
        adapter.setShowType(VoucherListSelectAdapter.SHOW_TYPE_SHOW);
    }

    private OnItemLongClickListener longListener = new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                                       int position, long id) {
            removeListViewChildIndex = position - 1;
            String status = String.valueOf(data.get(removeListViewChildIndex).get("status"));
            if (!"0".equals(status)) {
                String sysCouponID = String.valueOf(data.get(removeListViewChildIndex).get("couponID"));
                showDeleteDialog(sysCouponID);
            }
            return true;
        }
    };

    /** 删除对话框 */
    private void showDeleteDialog(final String sysCouponID) {
        final ConfirmDialog dialog = new ConfirmDialog(this, "提示", "是否删除这张抵用券？", "取消", "确定");
        dialog.show();
        dialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
            @Override
            public void doLeft() {
                dialog.dismiss();
            }

            @Override
            public void doRight() {
                requestDeleteVoucher(sysCouponID, dialog);
            }
        });
    }

    /** 删除消息 */
    private void requestDeleteVoucher(String sysCouponID, final ConfirmDialog dialog) {
        VoucherHttpBiz.deleteVoucher(sysCouponID, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                if (OrangeErrorHandler.getInstance().isSuccess(response)) {
                    data.remove(removeListViewChildIndex);
                    adapter.notifyDataSetChanged();
                    currentReqMode = REQ_MODE_INIT;
                    showProgress();
                    requestWhenPullRefresh();
                    ToastHelper.getInstance()._toast("删除成功");
                    dialog.dismiss();
                } else {
                    ToastHelper.getInstance()._toast("删除失败");
                }
            }
        });
    }


    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    ArrayList list = (ArrayList) JsonReponseHandler.getListFromJson(
                            String.valueOf(msg.obj), adapter, null);
                    int total = SurroundHttpBiz.getGoodListTotal(msg.obj);
                    handleAfterRequest(isConnected(), total, list, pullListView, adapter);
                    break;
                case 500:
                    dismissProgress();
                    ToastHelper.getInstance()._toast(
                            msg.obj.toString());
                    break;
                case 1011:
                    dismissProgress();
                    isLogin(dialog);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected EmptyLayout getEmptyLayout() {
        return emptyLayout;
    }

    @Override
    protected List getData() {
        return data;
    }

    @Override
    protected int getDataCount() {
        return data.size();
    }

    @Override
    protected void onResume() {
        super.onResume();
        closeActivity();
        BLog.getInstance().intAppmod(BLog.COUPON);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BLog.getInstance().outAppmod(BLog.COUPON);
    }
}
